package com.store.app.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.store.app.R;
import com.store.app.bean.TaskDetailsListBean;
import com.store.app.bean.TaskHeadBean;

import java.util.List;

/**
 * Created by Administrator on 2016/11/1.
 */

public class TaskDetialsListAdapter extends BaseAdapter {
    private List<TaskDetailsListBean> list;
    private Context context;
    final int TYPE_1 = 0;       //加载头布局view
    final int TYPE_2 = 1;       //加载尾布局view
    int tvlinecount=-1;          //行数
    boolean  iszhangkai=false;  //是否展开
    private TaskHeadBean taskHeadBean;
    public void setTaskHeadBean(TaskHeadBean taskHeadBean) {
        this.taskHeadBean = taskHeadBean;
    }
    private Handler handler = new Handler();
    public TaskDetialsListAdapter(Context context, List<TaskDetailsListBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size()+1;
    }

    @Override
    public Object getItem(int position) {

        return list.get(position);
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_1;
        } else  {
            return TYPE_2;
        }
    }

    @Override
    public int getViewTypeCount() {
        // TODO Auto-generated method stub
        return 2;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        int viewtype = getItemViewType(position);

        //加载头布局
        if (TYPE_1 == viewtype) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_task_head, null);
            TextView tv_content_title = (TextView) convertView.findViewById(R.id.tv_task_title);
            final TextView tv_content = (TextView) convertView.findViewById(R.id.tv_content);
            TextView tv_reward = (TextView) convertView.findViewById(R.id.tv_reward);
            TextView tv_expiration_date = (TextView) convertView.findViewById(R.id.tv_expiration_date);
            TextView tv_type = (TextView) convertView.findViewById(R.id.item_task_money_tpe);
            final ImageView img_arrow=(ImageView) convertView.findViewById(R.id.img_arrow);
            if(taskHeadBean!=null) {
                if ("gold".equals(taskHeadBean.getPayment_way_key())) {
                    tv_reward.setText(taskHeadBean.getTv_reward());
                    tv_type.setText("金币");
                } else {
                    tv_reward.setText(taskHeadBean.getTv_reward());
                    tv_type.setText("元");
                }
                tv_content_title.setText(taskHeadBean.getTv_content_title());
                tv_content.setText(taskHeadBean.getTv_content());
                tv_expiration_date.setText(taskHeadBean.getTv_expiration_date());
//                if (tvlinecount == -1) {
//                    try {
//                        //根据高度来判断是否需要再点击展开
//                        handler.post(new Runnable() {
//                            @Override
//                            public void run() {
//                                Log.i("mylog", "aa" + tv_content.getLineCount());
//                                tvlinecount = tv_content.getLineCount();
//                                if (tvlinecount <= 4) {
//                                    img_arrow.setVisibility(View.GONE);
//                                } else {
//                                    img_arrow.setVisibility(View.VISIBLE);
//                                    tv_content.setMaxLines(4);
//                                }
//                            }
//                        });
//                    } catch (Exception e) {
//
//                    }
//                } else {
//                    if (tvlinecount <= 4) {
//                        img_arrow.setVisibility(View.GONE);
//                    } else {
//                        img_arrow.setVisibility(View.VISIBLE);
//                        if (!iszhangkai) {
//                            img_arrow.setImageResource(R.drawable.task_arrow_down);
//                            tv_content.setMaxLines(4);
//                        } else {
//                            img_arrow.setImageResource(R.drawable.task_arrow_up);
//                            tv_content.setMaxLines(100);
//                        }
//                    }
//                }
//                img_arrow.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        Log.i("mylog", tvlinecount + "  " + iszhangkai);
//                        if (iszhangkai) {
//                            iszhangkai = false;
//                        } else {
//                            iszhangkai = true;
//                        }
//                        notifyDataSetChanged();
//                    }
//                });
            }
        } else {
            TaskDetailsListBean taskDetailsListBean = list.get(position-1);
            convertView = LayoutInflater.from(context).inflate(R.layout.item_task_mode, null);
            TextView tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            ImageView iv_picture = (ImageView) convertView.findViewById(R.id.iv_picture);
            tv_title.setText(taskDetailsListBean.getTitle());
            ImageLoader.getInstance().displayImage(taskDetailsListBean.getPicUrl(),iv_picture, options);
        }

        return convertView;
    }


    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();
}
